package com.insta.playdroid;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.util.Log;

import com.insta.playdroid.metier.domain.Song;
import com.insta.utils.SingletonPlayer;

public class Player implements IPlayer{

	private MediaPlayer mediaPlayer = SingletonPlayer.getInstancePlayer();
	private Song song;
	@SuppressWarnings("unused")
	private boolean flagNextOrPrevSong = false;
	public static final String ON = "ON";
	public static final String OFF = "OFF";
	private boolean repeatOne = false;
	private boolean repeatAll = false;
	private boolean random = false;
	private List<Integer> lastRandomIndexSong = new ArrayList<Integer>();
	private List<Song> songs;
	public static final int CODE_CURRENT_SONG = 0;
	public static final int CODE_ALBUM_SONG = 1;
	public static final int CODE_ALL_SONG = 2;
	public static final int CODE_ARTIST_SONG = 3;
	public int position = 0;
	int codeList;
	private Context context;
	public boolean callfromMenu;

	/*
	 * démarrer l'activité affichant la vue du lecteur
	 */
	/* (non-Javadoc)
	 * @see com.insta.playdroid.IPlayer#startPlayerActivity()
	 */
	@Override
	public void startPlayerActivity() {
		callfromMenu = false;
		Intent intent = new Intent(context, PlayerActivity.class);
		context.startActivity(intent);
	}
	
	@Override
	public void startPlayerActivity(boolean callfromMenu) {
		this.callfromMenu = callfromMenu;
		Intent intent = new Intent(context, PlayerActivity.class);
		context.startActivity(intent);
	}

	public Player(int position, List<Song> songs, Context context) {
		this.songs = songs;
		this.position = position;
		if (this.songs != null)
			this.song = songs.get(position);
		this.context = context;
	}

	/* (non-Javadoc)
	 * @see com.insta.playdroid.IPlayer#setSong(int)
	 */
	@Override
	public void setSong(int position) {
		this.song = songs.get(position);
	}

	/* (non-Javadoc)
	 * @see com.insta.playdroid.IPlayer#play()
	 */
	@Override
	public void play() {
		mediaPlayer.reset();
		try {
			mediaPlayer.setDataSource(song.getLocation());
			mediaPlayer.prepare();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		mediaPlayer.start();

	}

	/* (non-Javadoc)
	 * @see com.insta.playdroid.IPlayer#prevSong()
	 */
	@Override
	public Song prevSong() {
		int indexCurrentSong = -1;

		boolean dansLIste = songs.contains(song);
		Log.d("sdfs", "dansListe vaut : " + dansLIste);
		indexCurrentSong = songs.indexOf(song);

		if (indexCurrentSong != 0 && indexCurrentSong != -1) {
			song = songs.get(indexCurrentSong - 1);
			flagNextOrPrevSong = true;
			return song;
		} else {
			return null;// Toast.makeText(this, R.string.errorNoPrevSong,
						// Toast.LENGTH_SHORT)
			// .show();
		}
	}

	/* (non-Javadoc)
	 * @see com.insta.playdroid.IPlayer#nextSong()
	 */
	@Override
	public Song nextSong() {
		int indexCurrentSong = -1;
		boolean dansLIste = songs.contains(song);
		Log.d("sdfs", "dansListe vaut : " + dansLIste);
		indexCurrentSong = songs.indexOf(song);

		if (indexCurrentSong < songs.size() - 1 && !repeatOne && !random) {
			song = songs.get(indexCurrentSong + 1);
			Log.d("song suivante",
					song.getTitle() == null ? "no title" : song.getTitle());

			flagNextOrPrevSong = true;
			return song;
		} else {
			// repeat All media
			if (repeatAll == true) {
				song = songs.get(0);
				flagNextOrPrevSong = true;
				return song;
			} else if (repeatOne == true) {
				return song;
			} else if (random == true) {
				flagNextOrPrevSong = true;
				int sizeListSongs = songs.size();
				// save num song courante pour éviter de la rejouer
				if (lastRandomIndexSong.size() != sizeListSongs) {
					lastRandomIndexSong.add(songs.indexOf(song));
				}

				// tirer un num au hasard entre 0 et nb songs
				int randomIndexMusic = indexRandom();

				while (lastRandomIndexSong.contains(randomIndexMusic)
						&& sizeListSongs != lastRandomIndexSong.size()) {
					randomIndexMusic = indexRandom();
				}

				if (sizeListSongs != lastRandomIndexSong.size()) {
					song = songs.get(randomIndexMusic);
					return song;
				} else {
					return null;// Toast.makeText(this,
								// R.string.errorNoNextSong,
					// Toast.LENGTH_SHORT).show();
				}

			} else {
				return null;
				// Toast.makeText(this, R.string.errorNoNextSong,
				// Toast.LENGTH_SHORT).show();
			}

		}
	}

	private int indexRandom() {
		Random r = new Random();
		int randomIndexMusic = 0 + r.nextInt(songs.size() - 0);
		return randomIndexMusic;
	}

	/* (non-Javadoc)
	 * @see com.insta.playdroid.IPlayer#getCodeList()
	 */
	@Override
	public int getCodeList() {
		return codeList;
	}

	/* (non-Javadoc)
	 * @see com.insta.playdroid.IPlayer#setCodeList(int)
	 */
	@Override
	public void setCodeList(int codeList) {
		this.codeList = codeList;
	}

	/* (non-Javadoc)
	 * @see com.insta.playdroid.IPlayer#getSong()
	 */
	@Override
	public Song getSong() {
		return song;
	}

	/* (non-Javadoc)
	 * @see com.insta.playdroid.IPlayer#setSong(com.insta.playdroid.metier.domain.Song)
	 */
	@Override
	public void setSong(Song song) {
		this.song = song;
	}

	/* (non-Javadoc)
	 * @see com.insta.playdroid.IPlayer#isRepeatOne()
	 */
	@Override
	public boolean isRepeatOne() {
		return repeatOne;
	}

	/* (non-Javadoc)
	 * @see com.insta.playdroid.IPlayer#setRepeatOne(boolean)
	 */
	@Override
	public void setRepeatOne(boolean repeatOne) {
		this.repeatOne = repeatOne;
	}

	/* (non-Javadoc)
	 * @see com.insta.playdroid.IPlayer#isRepeatAll()
	 */
	@Override
	public boolean isRepeatAll() {
		return repeatAll;
	}

	/* (non-Javadoc)
	 * @see com.insta.playdroid.IPlayer#setRepeatAll(boolean)
	 */
	@Override
	public void setRepeatAll(boolean repeatAll) {
		this.repeatAll = repeatAll;
	}

	/* (non-Javadoc)
	 * @see com.insta.playdroid.IPlayer#isRandom()
	 */
	@Override
	public boolean isRandom() {
		return random;
	}

	/* (non-Javadoc)
	 * @see com.insta.playdroid.IPlayer#setRandom(boolean)
	 */
	@Override
	public void setRandom(boolean random) {
		this.random = random;
	}

	/* (non-Javadoc)
	 * @see com.insta.playdroid.IPlayer#getSongs()
	 */
	@Override
	public List<Song> getSongs() {
		return songs;
	}

	/* (non-Javadoc)
	 * @see com.insta.playdroid.IPlayer#setSongs(java.util.List)
	 */
	@Override
	public void setSongs(List<Song> songs) {
		this.songs = songs;
	}

	/* (non-Javadoc)
	 * @see com.insta.playdroid.IPlayer#getContext()
	 */
	@Override
	public Context getContext() {
		return context;
	}

	/* (non-Javadoc)
	 * @see com.insta.playdroid.IPlayer#setContext(android.content.Context)
	 */
	@Override
	public void setContext(Context context) {
		this.context = context;
	}

	public boolean isCallfromMenu() {
		return callfromMenu;
	}

	public void setCallfromMenu(boolean callfromMenu) {
		this.callfromMenu = callfromMenu;
	}
	
}
